﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate;
using NHibernate;
using FluentNHibernate.Cfg.Db;
using CID.Domain.Persistence.Mappings.Development;
using CID.Domain.Persistence.Conventions;

namespace CID.Domain.Persistence
{
    public static class Configuration
    {
        public static void Configure(NailsFramework.Persistence.NHibernate dataMapper)
        {
            dataMapper.Configure(x => 
            {
                var persistenceModel = new PersistenceModel();
                persistenceModel.AddMappingsFromAssembly(typeof(VersionMap).Assembly);
                persistenceModel.Conventions.AddFromAssemblyOf<ReferenceConvention>();
                persistenceModel.Configure(x);
            } 
            );
        }

        internal static ISessionFactory CreateSessionFactory(string connectionStringKey)
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringKey)))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<VersionMap>().Conventions.AddFromAssemblyOf<ReferenceConvention>())
                .BuildSessionFactory();
        }
    }
}
